﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarolLib.KVCompare
{
    public partial class frmGrid : Form
    {
        public string Title { get; set; }
        public List<KeyValuePair<string, string>> KeyValues { get; set; }

        string[] sp = { "::" };

        public frmGrid(string title, List<KeyValuePair<string,string>> kvps, string tag, ISaver saver)
        {
            InitializeComponent();
            this.Text = title;
            this.Title = title;
            this.KeyValues = kvps;
            this.btnSaveCSV.Tag = tag;
            this.btnSaveCSV.Click+=new EventHandler(saver.buttonSave_Click);
            this.btnSaveCSV.Click += new EventHandler(btnClose_Click);

            if (tag == "D")
            {
                List<DiffModel> list = new List<DiffModel>();
                kvps.ForEach(p => list.Add(new DiffModel()
                {
                    Key = p.Key,
                    CsvA = p.Value.Split(sp, StringSplitOptions.None)[0],
                    CsvB = p.Value.Split(sp, StringSplitOptions.None)[1]
                }));
                this.dataGridView1.DataSource = list;
            }
            else
                this.dataGridView1.DataSource = kvps;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        class DiffModel
        {
            public string Key { get; set; }
            public string CsvA { get; set; }
            public string CsvB { get; set; }
        }
    }
}
